/*
*    Description: Create the trigger on JNF_Batch__C for create the case automatically
*                  when fulfillmentnotes has the text and status is set fulfillment
*
*   Revision History:
*
*    Version                Author                Date                Description
*   1.0                    Bhargava.p            13/06/2012            Initial Draft
*
*/
public with sharing class ACF_CaseManipulator {
   
    //Variables
    private static final String FULFILLMENT = 'fulfillment';
    private static final String BATCH = 'Batch';
    private static final String STATUS_NEW = 'New';
    private static final String CASE_SUBJECT = 'Fulfillment Notes Case';
   
    //Property
    public String userProfileName{get; set;}
    public List<JNF_Batch__c> JNFBatchRecords{get; set;}
   
    //Constructor
    public ACF_CaseManipulator(list<JNF_Batch__c> pJNFBatchRecordList) {
        JNFBatchRecords = pJNFBatchRecordList;
        userProfileName = getProfileNameById(userinfo.getprofileid());
    }
   
    //Public Methods
    /* Creates a new Case record for each JNF_Batch__c record which statisfies the following
     * criteria:
     *    1. Fulfilment Notes <> Empty
     *    2. Status = 'Fulfilment'
     *  3. Owner Profile Name = 'Fulfillment'
     */
    public void createCaseOnJNFInsert() {
       
        List<JNF_Batch__c> jnfBatchRecList = new List<JNF_Batch__c>();
       
        for(JNF_Batch__c jnfBatchRec :  this.JNFBatchRecords){
            if(jnfBatchRec.fulfillment_notes__C != null &&
                  jnfBatchRec.Status__c.toLowerCase().compareTo(FULFILLMENT) == 0 ) {
               
                jnfBatchRecList.add(jnfBatchRec);
            }//if
        }//for
       
        if(!jnfBatchRecList.isEmpty()){ this.insertCaseRecords(jnfBatchRecList); }
    }
   
    /* Creates a new Case record for each JNF_Batch__c record which statisfies the following
     * criteria:
     *    1. Fulfilment Notes <> Empty
     *    2. Status = 'Fulfilment'
     *  3. Owner Profile Name = 'Fulfilment'
     *  4. Created_by_Batch_Trigger__c != true
     */
    public void createCaseOnJNFUpdate() {
       
        Map<Id, JNF_Batch__c> jnfBatchIdRecMap = new Map<Id, JNF_Batch__c>();
       
        for(JNF_Batch__c jnfBatchRec :  this.JNFBatchRecords){
            if(jnfBatchRec.fulfillment_notes__C != null &&
                  jnfBatchRec.Status__c.toLowerCase().compareTo(FULFILLMENT) == 0) {
               
                jnfBatchIdRecMap.put(jnfBatchRec.Id, jnfBatchRec);
            }//if
        }//for
       
        Map<Id, Boolean> JnfBatchIdCreatedBatchTriggerMap =
            getJNFIdBatchTriggerMap(jnfBatchIdRecMap.keySet());
            
            Set<Id> s1 = jnfBatchIdRecMap.keySet().clone();
            Set<Id> s2 = JnfBatchIdCreatedBatchTriggerMap.keySet().clone();
            s1.removeall(s2);
            
            for(id i : s1)
            {
            JnfBatchIdCreatedBatchTriggerMap.put(i,false);
            
            }
           
        List<JNF_Batch__c> createBatchFalseRecList = new List<JNF_Batch__c>();
        for(Id jnfBatchRecId : JnfBatchIdCreatedBatchTriggerMap.keySet()){
           
            if(!JnfBatchIdCreatedBatchTriggerMap.get(jnfBatchRecId)){
                createBatchFalseRecList.add(jnfBatchIdRecMap.get(jnfBatchRecId));
            }//if
        }   
       
        if(!createBatchFalseRecList.isEmpty()){ this.insertCaseRecords(createBatchFalseRecList); }
    }
   
    //Method to create for case
    public Database.Saveresult[] insertCaseRecords(List<JNF_Batch__c> pJnfBatchRecList) {
       
        //Check if the JnfBatch record Id Set is not empty
        if(pJnfBatchRecList.isEmpty()){ return NULL; }
       
        //Populate a list of Case records for the  associated JNF Batch Id Records
        List<Case> caseList = new List<Case>();       
        for(JNF_Batch__c jnfBatchRec : pJnfBatchRecList){
           
            Case newCase = new Case();           
            newcase.origin = BATCH;
            newcase.ownerid = jnfBatchRec.Owner__c;
            newcase.Description = jnfBatchRec.fulfillment_notes__c;
            newcase.Status = STATUS_NEW;
            newcase.Subject = BATCH + ' # ' + jnfBatchRec.Id +': Fulfillment Notes Case';
            newcase.JNF_Batch__c = jnfBatchRec.Id;
            newcase.Created_by_Batch_Trigger__c = true;
           
            caseList.add(newCase);
        }//for
       
        //Set a Savepoint
        System.savepoint insSavePoint = Database.setSavePoint();
       
        try{
            Database.Saveresult[] saveResultList = Database.insert(caseList);
            return saveResultList;
        }
        catch(System.DmlException dmlException){
            System.debug('******Case Insert Excerption: ' + dmlException.getMessage());
            Database.rollback(insSavePoint);
            return NULL;
        }
        
    }
       
    //Static Methods
    /* Returns the Profile record for the specified input profile Record id*/
    private static String getProfileNameById(String pProfileId){
        return [Select Id, Name from Profile Where Id = :pProfileId][0].Name;
    }
   
    /* Creates a Map of JNF Rec Id and a Boolean value of Created_by_Batch_Trigger__c field */
    private static Map<Id, Boolean> getJNFIdBatchTriggerMap(Set<Id> pJnfBatchIdSet){
       
        Map<Id, Boolean> jnfCreatedByBatchTriggerMap = new Map<Id, Boolean>();
       
        for(Case oCase : [Select Id,
                                 Created_by_Batch_Trigger__c,
                                 JNF_Batch__c
                          From Case
                          Where JNF_Batch__c IN :pJnfBatchIdSet]){
           
            jnfCreatedByBatchTriggerMap.put(oCase.JNF_Batch__c, oCase.Created_by_Batch_Trigger__c);
        }//for
        return jnfCreatedByBatchTriggerMap;
    }
   
}//ACF_JnfBatchManipulator